package com.itc.display;

import java.io.InputStream;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.midlet.MIDletStateChangeException;

import com.itc.resource.LoadResource;

public class CanvasMain extends GameCanvas implements CommandListener {
	private Main midlet;
	private int width;
	private int height;
	private Player player;
	private String path14Hz, path16Hz, path20Hz, path;
	private int mSizeText;
	private boolean mChoose14, mChoose16, mChoose20, mChoosePlay;
	private Command mCmdBack;
	private String mFrequenry, mTitle;

	protected CanvasMain(Main midlet) {
		super(true);
		this.midlet = midlet;
		initViews();
		path14Hz = "/mosquito_14kHz.mp3";
		path16Hz = "/mosquito_16kHz.mp3";
		path20Hz = "/mosquito_20kHz.mp3";
		mChoose14 = true;
		mChoose16 = false;
		mChoose20 = false;
		path = path14Hz;
		mTitle = "MOSQUITO SHIELD";
		mFrequenry = "Play 14K Frequency...";
		initSound(path);
		mChoosePlay = false;
	}

	public void initViews() {
		width = getWidth();
		height = getHeight();
		mCmdBack = new Command("Back", Command.BACK, 1);
		addCommand(mCmdBack);
		this.setCommandListener(this);
	}

	private void initSound(String urlSound) {
		try {
			InputStream is = getClass().getResourceAsStream(urlSound);
			player = Manager.createPlayer(is, "audio/mpeg");
			player.realize();
			VolumeControl vc = (VolumeControl) player
					.getControl("VolumeControl");
			if (vc == null) {
				vc.setLevel(100);
			}
			player.setLoopCount(-1);
		} catch (Exception ex) {
			System.err.println("Error: " + ex.getMessage());
		}
	}

	private void playSound() {
		try {
			player.start();
		} catch (MediaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void desSound() {
		try {
			player.stop();
		} catch (MediaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		player.close();
	}

	public void paint(Graphics g) {

		g.setColor(0x000);

		g.setColor(0, 0, 0);

		g.fillRect(0, 0, width, height);

		g.drawImage(LoadResource.mImgExit, width - 20 - 5, 5, Graphics.TOP
				| Graphics.LEFT);

		g.drawImage(LoadResource.mImgAbout, width - 20 - 5 - 20 - 5, 5,
				Graphics.TOP | Graphics.LEFT);

		g.drawImage(LoadResource.mImgHelp, width - 20 - 5 - 20 - 5 - 20 - 5, 5,
				Graphics.TOP | Graphics.LEFT);
		g.setColor(255, 255, 255);
		Font font = g.getFont();
		mSizeText = font.stringWidth("16kHz");
		int mSizeFrequenry = font.stringWidth("Play 14K Frequency...");
		int mSizeTitle = font.stringWidth(mTitle);
		g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC,
				Font.SIZE_SMALL));
		
		g.drawString(mFrequenry, (width - mSizeFrequenry) / 2,
				(height - 140) / 2 + 145, Graphics.TOP | Graphics.LEFT);
		g.setColor(205, 141, 1);
		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
				Font.SIZE_LARGE));
		
		g.drawString(mTitle, (width - mSizeTitle) / 2, 35, Graphics.TOP
				| Graphics.LEFT);

		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
				Font.SIZE_LARGE));
		if (mChoose14)
			g.setColor(150, 100, 150);
		else
			g.setColor(255, 255, 255);
		g.drawString("14kHz", (width - mSizeText) / 2 - mSizeText - 20, height
				- Font.SIZE_LARGE - 10, Graphics.TOP | Graphics.LEFT);
		if (mChoose16)
			g.setColor(150, 100, 150);
		else
			g.setColor(255, 255, 255);
		g.drawString("16kHz", (width - mSizeText) / 2, height - Font.SIZE_LARGE
				- 10, Graphics.TOP | Graphics.LEFT);
		if (mChoose20)
			g.setColor(150, 100, 150);
		else
			g.setColor(255, 255, 255);
		g.drawString("20kHz", (width - mSizeText) / 2 + mSizeText + 20, height
				- Font.SIZE_LARGE - 10, Graphics.TOP | Graphics.LEFT);
		if (mChoosePlay) {
			g.drawImage(LoadResource.mImgMosPressed, (width - 140) / 2,
					(height - 140) / 2, Graphics.TOP | Graphics.LEFT);
		} else
			g.drawImage(LoadResource.mImgMosNormal, (width - 140) / 2,
					(height - 140) / 2, Graphics.TOP | Graphics.LEFT);
	}

	protected void pointerPressed(int x, int y) {
		// button mosquito
		if (x >= (width - 140) / 2 && y >= (height - 140) / 2
				&& x <= (width - 140) / 2 + 140
				&& y <= (height - 140) / 2 + 140) {
			if (mChoosePlay) {
				try {
					desSound();
				} catch (Exception e) {
					// TODO: handle exception
				}
				mChoosePlay = false;
			} else {
				mChoosePlay = true;
				initSound(path);
				playSound();
			}
			repaint();

		}
		// button exit
		if (x >= width - 20 - 5 && y >= 5 && x <= width - 20 - 5 + 20
				&& y <= 5 + 20) {
			try {
				this.midlet.destroyApp(true);
			} catch (Exception e) {
			}
		}
		// button about
		if (x >= width - 20 - 5 - 20 - 5 && y >= 5
				&& y <= width - 20 - 5 - 20 - 5 + 20 && y <= 5 + 20) {
			AlertDialog alertDialog = new AlertDialog();
			Alert confirmationAlert = alertDialog
					.createSetupAlert(
							null,
							"ABOUT",
							"\nMosquito Shield\nVersion: 1.0.0\nDeveloped by ITC Group",
							AlertType.CONFIRMATION);
			Display display = Display.getDisplay(midlet);
			display.setCurrent(confirmationAlert);
		}
		// button help
		if (x >= width - 20 - 5 - 20 - 5 - 20 - 5 && y >= 5
				&& x <= width - 20 - 5 - 20 - 5 - 20 - 5 + 20 && y <= 5 + 20) {
			AlertDialog alertDialog = new AlertDialog();
			Alert confirmationAlert = alertDialog
					.createSetupAlert(
							null,
							"HELP",
							"Feature three frequencies 14kHz, 16kHz and 20kHz. Try out which one works best in your area and pressed center button.",
							AlertType.CONFIRMATION);
			Display display = Display.getDisplay(midlet);
			display.setCurrent(confirmationAlert);
		}
		// button 16kHz
		if (x >= (width - mSizeText) / 2 && y >= height - Font.SIZE_LARGE - 10
				&& x <= (width - mSizeText) / 2 + mSizeText
				&& y <= height - Font.SIZE_LARGE - 10 + Font.SIZE_LARGE) {
			mChoose16 = true;
			mChoose14 = false;
			mChoose20 = false;
			mChoosePlay = false;
			mFrequenry = "Play 16K Frequency...";
			try {
				desSound();
			} catch (Exception e) {
				// TODO: handle exception
			}
			path = path16Hz;
			repaint();
		}
		// button 14kHz
		if (x >= (width - mSizeText) / 2 - mSizeText - 20
				&& y >= height - Font.SIZE_LARGE - 10
				&& x <= (width - mSizeText) / 2 - mSizeText - 20 + mSizeText
				&& y <= height - Font.SIZE_LARGE - 10 + Font.SIZE_LARGE) {
			mChoose16 = false;
			mChoose14 = true;
			mChoosePlay = false;
			mChoose20 = false;
			mFrequenry = "Play 14K Frequency...";
			try {
				desSound();
			} catch (Exception e) {
				// TODO: handle exception
			}
			path = path14Hz;
			repaint();
		}
		// button 20kHz
		if (x >= (width - mSizeText) / 2 + mSizeText + 20
				&& y >= height - Font.SIZE_LARGE - 10
				&& x <= (width - mSizeText) / 2 + mSizeText + 20 + mSizeText
				&& y <= height - Font.SIZE_LARGE - 10 + Font.SIZE_LARGE) {
			mChoose16 = false;
			mChoose14 = false;
			mChoosePlay = false;
			mChoose20 = true;
			mFrequenry = "Play 20K Frequency...";
			try {
				desSound();
			} catch (Exception e) {
			}
			path = path20Hz;
			repaint();
		}

	}

	public void commandAction(Command c, Displayable d) {
		if (c == mCmdBack)
			try {
				this.midlet.destroyApp(true);
			} catch (MIDletStateChangeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}
